﻿using System;
using SystemModule;

namespace M2Server
{
    public partial class TPlayObject
    {
        /// <summary>
        /// 杀怪取得经验
        /// </summary>
        /// <param name="dwExp"></param>
        private void WinExp(int dwExp)
        {
            try
            {
                if (this.m_Abil.Level > M2Share.g_Config.nLimitExpLevel) // 超过限制等级,只给指定的限制经验
                {
                    dwExp = M2Share.g_Config.nLimitExpValue;
                    GetExp(dwExp, false, 0);
                }
                else if (dwExp > 0)
                {
                    if (m_nKillMonExpRate <= 0)
                    {
                        m_nKillMonExpRate = 100; // 防止倍数为负数
                    }
                    dwExp = M2Share.g_Config.dwKillMonExpMultiple * dwExp; // 系统指定杀怪经验倍数
                    dwExp = (int)HUtil32.Round((double)(m_nKillMonExpRate / 100) * dwExp, 1); // 人物指定的杀怪经验倍数
                    if (this.m_PEnvir != null)
                    {
                        if (this.m_PEnvir.m_boEXPRATE)
                        {
                            dwExp = (int)HUtil32.Round((double)(this.m_PEnvir.m_nEXPRATE / 100) * dwExp, 1); // 地图上指定杀怪经验倍数
                        }
                    }
                    if (this.m_boExpItem) // 物品经验倍数
                    {
                        dwExp = (int)HUtil32.Round((double)this.m_rExpItem * dwExp, 1);
                    }
                    GetExp(dwExp, false, 0);
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TPlayObject.WinExp");
            }
        }

        /// <summary>
        /// 取得内力经验
        /// </summary>
        /// <param name="dwExp"></param>
        /// <param name="Code">0-杀怪分配 1-非杀怪分配 2-饮酒(谁喝分配给谁) 3-提取天地结晶经验</param>
        public void GetNGExp(int dwExp, byte Code)
        {
            try
            {
                if (this.m_Abil.Level > M2Share.g_Config.nLimitExpLevel) // 超过限制等级,只给指定的限制经验
                {
                    dwExp = M2Share.g_Config.nLimitExpValue;
                }
                else if ((dwExp > 0) && (Code == 0))
                {
                    dwExp = M2Share.g_Config.dwKillMonExpMultiple * dwExp;// 系统指定杀怪经验倍数
                    dwExp = (int)HUtil32.Round((double)(m_nKillMonExpRate / 100) * dwExp, 1);// 人物指定的杀怪经验倍数
                    if (this.m_PEnvir != null)
                    {
                        if (this.m_PEnvir.m_boEXPRATE)// 地图上指定杀怪经验倍数
                        {
                            dwExp = (int)HUtil32.Round((double)(this.m_PEnvir.m_nEXPRATE / 100) * dwExp, 1);
                        }
                    }
                    if (this.m_boExpItem) // 物品经验倍数
                    {
                        dwExp = (int)HUtil32.Round((double)this.m_rExpItem * dwExp, 1);
                    }
                }
                if (m_MyHero != null)
                {
                    if ((!m_MyHero.m_boDeath) && (!m_MyHero.m_boGhost))
                    {
                        switch (Code)
                        {
                            case 0:// 杀怪内功经验倍数
                                ((THeroObject)(m_MyHero)).GetNGExp((int)Math.Abs(HUtil32.Round((double)(M2Share.g_Config.nHeroKillMonExpRate / 100) * dwExp, 1)), 3);
                                dwExp = (int)Math.Abs(HUtil32.Round((double)((100 - M2Share.g_Config.nHeroKillMonExpRate) / 100) * dwExp, 1));
                                dwExp = (int)Math.Abs(HUtil32.Round((double)dwExp * M2Share.g_Config.dwKillMonNGExpMultiple / 100, 1));
                                break;
                            case 1:// 非杀怪,英雄与主人一样经验
                                ((THeroObject)(m_MyHero)).GetNGExp(dwExp, 1);
                                break;
                            case 3:// 提取天地结晶内功经验，英雄分配比例
                                ((THeroObject)(m_MyHero)).GetNGExp((int)Math.Abs(HUtil32.Round((double)(M2Share.g_Config.nHeroCrystalExpRate / 100) * dwExp, 1)), 2);
                                dwExp = (int)Math.Abs(HUtil32.Round((double)((100 - M2Share.g_Config.nHeroCrystalExpRate) / 100) * dwExp, 1));
                                break;
                        }
                    }
                }
                else
                {
                    if ((dwExp > 0) && (Code == 0))
                    {
                        dwExp = (int)Math.Abs(HUtil32.Round((double)dwExp * M2Share.g_Config.dwKillMonNGExpMultiple / 100, 1));// 杀怪内功经验倍数
                    }
                }
                if (m_boTrainingNG && (dwExp > 0))
                {
                    if (m_ExpSkill69 >= dwExp)
                    {
                        if ((int.MaxValue - m_ExpSkill69) < dwExp)
                        {
                            dwExp = int.MaxValue - m_ExpSkill69;
                        }
                    }
                    else
                    {
                        if ((int.MaxValue - dwExp) < m_ExpSkill69)
                        {
                            dwExp = int.MaxValue - dwExp;
                        }
                    }
                    m_ExpSkill69 += dwExp; // 内功心法当前经验
                    if (!m_boNotOnlineAddExp)
                    {
                        this.SendMsg(this, Grobal2.RM_WINNHEXP, 0, dwExp, 0, 0, "");
                    }
                    // 只发送给非离线挂机人物
                    if (m_ExpSkill69 >= m_MaxExpSkill69)
                    {
                        m_ExpSkill69 -= m_MaxExpSkill69;
                        m_NGLevel++;
                        m_MaxExpSkill69 = this.GetSkill69Exp(m_NGLevel, ref m_Skill69MaxNH);// 取内功心法升级经验
                        m_Skill69NH = m_Skill69MaxNH;
                        this.SendRefMsg(Grobal2.RM_MAGIC69SKILLNH, 0, m_Skill69NH, m_Skill69MaxNH, 0, "");// 内力值让别人看到
                        this.SendRefMsg(Grobal2.RM_MYSHOW, Grobal2.ET_OBJECTLEVELUP, 0, 0, 0, "");// 人物升级动画 
                        NGLevelUpFunc();// 内功升级触发
                    }
                    this.SendNGData();
                    if (Code != 3)
                    {
                        GetExpToCrystal(dwExp, 1); // 取经验到天地结晶中
                    }
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TPlayObject.GetNGExp");
            }
        }

        /// <summary>
        /// 内功技能升级
        /// </summary>
        /// <param name="UserMagic"></param>
        public void NGMagic_Lvexp(TUserMagic UserMagic)
        {
            if ((UserMagic != null))
            {
                if ((this.m_btRaceServer == Grobal2.RC_PLAYOBJECT) && (UserMagic.btLevel < 3) && (UserMagic.MagicInfo.TrainLevel[UserMagic.btLevel] <= m_NGLevel))
                {
                    this.TrainSkill(UserMagic, HUtil32.Random(3) + 1);
                    if (!this.CheckMagicLevelup(UserMagic))
                    {
                        this.SendDelayMsg(this, Grobal2.RM_MAGIC_LVEXP, 0, UserMagic.MagicInfo.wMagicId, UserMagic.btLevel, UserMagic.nTranPoint, "", 3000);
                    }
                }
            }
        }

        /// <summary>
        /// 取得经验
        /// </summary>
        /// <param name="dwExp"></param>
        /// <param name="boItmeExp">False-将经验累积到聚灵珠中 True-不累积经验到聚灵珠中</param>
        /// <param name="nType">3-提取天地结晶经验</param>
        public void GetExp(int dwExp, bool boItmeExp, byte nType)
        {
            byte nCode = 0;
            try
            {
                if (this.m_Abil.Exp >= dwExp)
                {
                    if ((int.MaxValue - this.m_Abil.Exp) < dwExp)
                    {
                        dwExp = int.MaxValue - this.m_Abil.Exp;
                    }
                }
                else
                {
                    if ((int.MaxValue - dwExp) < this.m_Abil.Exp)
                    {
                        dwExp = int.MaxValue - dwExp;
                    }
                }
                if (!boItmeExp)
                {
                    GetExpToItem(dwExp); // 取得的经验,累积到聚灵珠
                }
                nCode = 11;
                if (m_MyHero != null) // 人取得经验,都给英雄分配经验
                {
                    nCode = 12;
                    if ((!m_MyHero.m_boDeath) && (!m_MyHero.m_boGhost))
                    {
                        nCode = 10;
                        switch (nType)
                        {
                            case 3:// 提取天地结晶经验
                                ((THeroObject)(m_MyHero)).GetExp((int)Math.Abs(HUtil32.Round((double)M2Share.g_Config.nHeroCrystalExpRate / 100 * dwExp)));
                                dwExp = (int)Math.Abs(HUtil32.Round(100 - (double)M2Share.g_Config.nHeroCrystalExpRate / 100 * dwExp));
                                break;
                            default:
                                ((THeroObject)(m_MyHero)).GetExp((int)Math.Abs(HUtil32.Round((double)M2Share.g_Config.nHeroKillMonExpRate / 100 * dwExp)));
                                dwExp = (int)Math.Abs(HUtil32.Round(100 - (double)M2Share.g_Config.nHeroKillMonExpRate / 100 * dwExp));
                                break;
                        }
                    }
                }
                nCode = 3;
                m_GetExp = dwExp;// 人物取得的经验,$GetExp变量使用
                if (M2Share.g_FunctionNPC != null)
                {
                    M2Share.g_FunctionNPC.GotoLable(this, "@GetExp", false); // 经验触发
                }
                this.m_Abil.Exp += dwExp;
                this.AddBodyLuck(dwExp * 0.002);
                if (!m_boNotOnlineAddExp) // 只发送给非离线挂机人物
                {
                    this.SendMsg(this, Grobal2.RM_WINEXP, 0, dwExp, 0, 0, "");
                }
                nCode = 4;
                if (this.m_Abil.Exp >= this.m_Abil.MaxExp)
                {
                    this.m_Abil.Exp -= this.m_Abil.MaxExp;
                    if ((this.m_Abil.Level < M2Share.MAXUPLEVEL) && (this.m_Abil.Level < M2Share.g_Config.nLimitExpLevel))
                    {
                        this.m_Abil.Level++;
                    }
                    // 增加限制等级
                    if (this.m_Abil.Level < M2Share.g_Config.nLimitExpLevel)
                    {
                        this.HasLevelUp(this.m_Abil.Level - 1);//  增加限制等级
                    }
                    nCode = 5;
                    this.AddBodyLuck(100);// 人物升级记录日志
                    M2Share.AddGameDataLog("12" + "\09" + this.m_sMapName + "\09" + (this.m_Abil.Level).ToString() + "\09" + (this.m_Abil.Exp).ToString()
                        + "/" + (this.m_Abil.MaxExp).ToString() + "\09" + this.m_sCharName + "\09" + "0" + "\09" + "0" + "\09" + "1" + "\09" + "0");
                    nCode = 6;
                    this.IncHealthSpell(2000, 2000);
                }
                nCode = 7;
                if (this.m_MagicArr[0][MagicConst.SKILL_68] != null)// 学过酒气护体
                {
                    if (this.m_MagicArr[0][MagicConst.SKILL_68].btLevel < 100)
                    {
                        m_Exp68 += (UInt16)dwExp;
                    }
                    if (m_Exp68 >= m_MaxExp68) // 超过升级经验,则升级技能
                    {
                        m_Exp68 -= m_MaxExp68;
                        if (this.m_MagicArr[0][MagicConst.SKILL_68].btLevel < 100)
                        {
                            this.m_MagicArr[0][MagicConst.SKILL_68].btLevel++;
                        }
                        nCode = 8;
                        m_MaxExp68 = (UInt16)this.GetSkill68Exp(this.m_MagicArr[0][MagicConst.SKILL_68].btLevel);
                        this.SendDelayMsg(this, Grobal2.RM_MAGIC_LVEXP, 0, this.m_MagicArr[0][MagicConst.SKILL_68].MagicInfo.wMagicId, this.m_MagicArr[0][MagicConst.SKILL_68].btLevel, this.m_MagicArr[0][MagicConst.SKILL_68].nTranPoint, "", 100);
                    }
                    nCode = 9;
                    if ((this != null) && (this.m_MagicArr[0][MagicConst.SKILL_68].btLevel < 100))// 发送酒气护体经验
                    {
                        this.SendMsg(this, Grobal2.RM_MAGIC68SKILLEXP, 0, 0, 0, 0, EncryptUnit.EncodeString(m_Exp68 + "/" + m_MaxExp68)); // 发消息更新客户端显示
                    }
                }
                if ((nType != 3))
                {
                    GetExpToCrystal(dwExp, 0);//取经验到天地结晶中
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TPlayObject.GetExp Code:" + nCode);
            }
        }

        /// <summary>
        /// 客户端提取天地结晶经验
        /// </summary>
        /// <param name="dwExp"></param>
        /// <param name="nType"> 0-经验 1-内功经验</param>
        public void GetExpToCrystal(int dwExp, byte nType)
        {
            if (m_boShowExpCrystal)
            {
                switch (nType)
                {
                    case 0:// 天地结晶图标为显示时 经验
                        if ((m_CrystalMaxExp == 0) && (m_CrystalLevel < 5))  // 天地结晶等级为0时，取当前经验的上限值
                        {
                            GetExpCrystal(m_CrystalLevel, ref m_CrystalMaxExp, ref m_CrystalNGMaxExp);// 取天地结晶当前等级经验上限
                        }
                        if (m_CrystalLevel < 5)
                        {
                            m_CrystalExp += dwExp; // 天地结晶当前经验
                        }
                        if ((m_CrystalExp >= m_CrystalMaxExp) && (m_CrystalNGExp >= m_CrystalNGMaxExp) && (m_CrystalLevel < 5))
                        {
                            // 超过升级经验,并且内功经验超过升级经验,则天地结晶升级
                            if (m_CrystalLevel < 5)
                            {
                                m_boGetExpCrystalExp = true;// 是否可以提取经验
                                m_nGetCrystalExp = m_CrystalMaxExp;// 可提取天地结晶经验
                                m_nGetCrystalNGExp = m_CrystalNGMaxExp;// 天可提取地结晶内功经验
                                m_CrystalLevel++;
                            }
                            if (m_CrystalLevel < 5)
                            {
                                GetExpCrystal(m_CrystalLevel, ref m_CrystalMaxExp, ref m_CrystalNGMaxExp); // 取天地结晶当前等级经验上限
                            }
                            SendDefMessage(Grobal2.SM_SENDCRYSTALLEVEL, m_CrystalLevel, 0, 0, 0, "");// 发送天地结晶的等级
                        }
                        if ((this != null) && (m_CrystalLevel < 5))
                        {
                            SendDefMessage(Grobal2.SM_SENDCRYSTALEXP, 0, 0, 0, 0, m_CrystalExp + "/" + m_CrystalMaxExp + "/" + m_CrystalNGMaxExp);
                        }
                        break;
                    case 1:// 发送天地结晶经验 内功经验
                        if ((m_CrystalNGMaxExp == 0) && (m_CrystalLevel < 5))// 取当前经验的上限值
                        {
                            GetExpCrystal(m_CrystalLevel, ref m_CrystalMaxExp, ref m_CrystalNGMaxExp);// 取天地结晶当前等级经验上限
                        }
                        if (m_CrystalLevel < 5)
                        {
                            m_CrystalNGExp += dwExp;// 天地结晶当前经验
                        }
                        if ((m_CrystalExp >= m_CrystalMaxExp) && (m_CrystalNGExp >= m_CrystalNGMaxExp) && (m_CrystalLevel < 5))// 超过升级经验,并且内功经验超过升级经验,则天地结晶升级
                        {
                            if (m_CrystalLevel < 5)
                            {
                                m_boGetExpCrystalExp = true;// 是否可以提取经验 
                                m_nGetCrystalExp = m_CrystalMaxExp;// 可提取天地结晶经验 
                                m_nGetCrystalNGExp = m_CrystalNGMaxExp;// 天可提取地结晶内功经验 
                                m_CrystalLevel++;
                            }
                            if (m_CrystalLevel < 5)
                            {
                                GetExpCrystal(m_CrystalLevel, ref m_CrystalMaxExp, ref m_CrystalNGMaxExp); // 取天地结晶当前等级经验上限
                            }
                            SendDefMessage(Grobal2.SM_SENDCRYSTALLEVEL, m_CrystalLevel, 0, 0, 0, "");// 发送天地结晶的等级
                        }
                        if ((this != null) && (m_CrystalLevel < 5))
                        {
                            SendDefMessage(Grobal2.SM_SENDCRYSTALNGEXP, 0, 0, 0, 0, m_CrystalNGExp + "/" + m_CrystalMaxExp + "/" + m_CrystalNGMaxExp);
                        }
                        break;
                }
            }
        }

        /// <summary>
        /// 取得的经验,累积到物品上,-----聚灵珠
        /// </summary>
        /// <param name="dwExp"></param>
        public void GetExpToItem(int dwExp)
        {
            TUserItem UserItem = null;
            TStdItem AmuletStdItem;
            bool boGetExpToItem;
            bool boHeroToItem;
            byte nCode;
            nCode = 0;
            boGetExpToItem = false;
            boHeroToItem = false;
            try
            {
                if (this.m_boDeath || this.m_boGhost)// 死亡不再聚
                {
                    return;
                }
                nCode = 1;
                if (this.m_ItemList.Count > 0)
                {
                    nCode = 11;
                    for (int i = 0; i < this.m_ItemList.Count; i++) // 人物包裹不为空
                    {
                        nCode = 12;
                        UserItem = this.m_ItemList[i];
                        if (UserItem != null)
                        {
                            nCode = 13;
                            AmuletStdItem = M2Share.UserEngine.GetStdItem(UserItem.wIndex);
                            if (AmuletStdItem != null)
                            {
                                if ((AmuletStdItem.StdMode == 51) && (AmuletStdItem.Shape == 0) && (UserItem.btValue[12] != 2) && (UserItem.Dura < UserItem.DuraMax))
                                {
                                    // 聚灵珠
                                    nCode = 3;
                                    if (this.m_PEnvir != null)
                                    {
                                        if (!this.m_PEnvir.AllowStdItems(AmuletStdItem.Name)) // 检查地图是否禁用聚灵珠
                                        {
                                            return;
                                        }
                                    }
                                    nCode = 34;
                                    if (UserItem.btValue[20] == 1)
                                    {
                                        boGetExpToItem = true;
                                    }
                                    if (n_UsesItemTick == 0) // 24 * 60 * 60
                                    {
                                        n_UsesItemTick = AmuletStdItem.AniCount * 86400;// 使用时间
                                        boGetExpToItem = true;
                                        UserItem.btValue[20] = 1;
                                    }
                                    if (boGetExpToItem)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                nCode = 2;
                if (!boGetExpToItem && (m_MyHero != null))
                {
                    nCode = 21;
                    if (m_MyHero.m_boDeath || m_MyHero.m_boGhost) // 死亡不再聚
                    {
                        return;
                    }
                    nCode = 22;
                    if (m_MyHero.m_ItemList != null)
                    {
                        nCode = 23;
                        if (m_MyHero.m_ItemList.Count > 0)
                        {
                            nCode = 24;
                            for (int i = 0; i < m_MyHero.m_ItemList.Count; i++)// 人物包里没有珠,英雄包裹不为空时
                            {
                                nCode = 25;
                                UserItem = m_MyHero.m_ItemList[i];
                                if (UserItem != null)
                                {
                                    nCode = 26;
                                    AmuletStdItem = M2Share.UserEngine.GetStdItem(UserItem.wIndex);
                                    if (AmuletStdItem != null)
                                    {
                                        if ((AmuletStdItem.StdMode == 51) && (AmuletStdItem.Shape == 0) &&
                                            (UserItem.btValue[12] != 2) && (UserItem.Dura < UserItem.DuraMax)) // 聚灵珠
                                        {
                                            nCode = 32;
                                            if (m_MyHero.m_PEnvir != null)
                                            {
                                                if (!m_MyHero.m_PEnvir.AllowStdItems(AmuletStdItem.Name))// 检查地图是否禁用聚灵珠
                                                {
                                                    return;
                                                }
                                            }
                                            nCode = 33;
                                            if (UserItem.btValue[20] == 1)
                                            {
                                                boHeroToItem = true;
                                            }
                                            if (n_UsesItemTick == 0)
                                            {
                                                n_UsesItemTick = AmuletStdItem.AniCount * 86400;// 使用时间 24 * 60 * 60
                                                boHeroToItem = true;
                                                UserItem.btValue[20] = 1;
                                            }
                                            if (boHeroToItem)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                nCode = 4;
                if (this.m_nWinExp >= dwExp)
                {
                    if ((int.MaxValue - this.m_nWinExp) < dwExp)
                    {
                        dwExp = int.MaxValue - this.m_nWinExp;
                    }
                }
                else
                {
                    if ((int.MaxValue - dwExp) < this.m_nWinExp)
                    {
                        dwExp = int.MaxValue - dwExp;
                    }
                }
                if (boGetExpToItem)// 主人包裹里聚经验
                {
                    this.m_nWinExp += dwExp;
                    if (this.m_nWinExp >= 10000)
                    {
                        if ((UserItem.Dura + this.m_nWinExp / 10000) > UserItem.DuraMax)
                        {
                            while (true)
                            {
                                if (this.m_nWinExp < 10000)
                                {
                                    break;
                                }
                                UserItem.Dura++;
                                this.m_nWinExp -= 10000;
                                if (UserItem.Dura >= UserItem.DuraMax)
                                {
                                    UserItem.Dura = UserItem.DuraMax;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            UserItem.Dura += Convert.ToUInt16(this.m_nWinExp / 10000);
                            this.m_nWinExp = this.m_nWinExp % 10000;
                            if (UserItem.Dura >= UserItem.DuraMax)
                            {
                                UserItem.Dura = UserItem.DuraMax;
                            }
                        }
                        this.SendMsg(this, Grobal2.RM_DURACHANGE, UserItem.wIndex, UserItem.Dura, UserItem.DuraMax, 0, "");
                        SendUpdateItem(UserItem);// 更新物品
                        SendItemUseTime(n_UsesItemTick);//发送物品使用时间
                    }
                    if (UserItem.Dura >= UserItem.DuraMax)
                    {
                        UserItem.Dura = UserItem.DuraMax;
                        n_UsesItemTick = 0;
                        UserItem.btValue[12] = 2;//不能聚集的标识
                        SendItemUseTime(n_UsesItemTick);// 发送聚灵珠使用剩余时间
                        this.SysMsg("【聚灵珠】已聚集足够的经验，请双击使用!", TMsgColor.c_Green, TMsgType.t_Hint);
                    }
                }
                else if (boHeroToItem)// 英雄包裹里,珠子聚经验
                {
                    nCode = 5;
                    this.m_nWinExp += dwExp;
                    if (this.m_nWinExp >= 10000)
                    {
                        if ((UserItem.Dura + this.m_nWinExp / 10000) > UserItem.DuraMax)
                        {
                            while (true)
                            {
                                if (this.m_nWinExp < 10000)
                                {
                                    break;
                                }
                                UserItem.Dura++;
                                this.m_nWinExp -= 10000;
                                if (UserItem.Dura >= UserItem.DuraMax)
                                {
                                    UserItem.Dura = UserItem.DuraMax;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            UserItem.Dura += Convert.ToUInt16(this.m_nWinExp / 10000);
                            this.m_nWinExp = this.m_nWinExp % 10000;
                            if (UserItem.Dura >= UserItem.DuraMax)
                            {
                                UserItem.Dura = UserItem.DuraMax;
                            }
                        }
                        ((THeroObject)(m_MyHero)).SendMsg(m_MyHero, Grobal2.RM_HERODURACHANGE, UserItem.wIndex, UserItem.Dura, UserItem.DuraMax, 0, "");
                        ((THeroObject)(m_MyHero)).SendUpdateItem(UserItem);// 更新物品
                        SendItemUseTime(n_UsesItemTick);//发送物品剩余时间
                        ((THeroObject)(m_MyHero)).SendUpdateItem(UserItem);// 更新物品
                    }
                    if (UserItem.Dura >= UserItem.DuraMax)
                    {
                        UserItem.Dura = UserItem.DuraMax;
                        n_UsesItemTick = 0;
                        UserItem.btValue[12] = 2;//不能聚集的标识
                        SendItemUseTime(n_UsesItemTick);// 发送聚灵珠使用剩余时间
                        this.SysMsg("【聚灵珠】已聚集足够的经验，请双击使用!", TMsgColor.c_Green, TMsgType.t_Hint);
                    }
                }
                nCode = 6;
                if (!boGetExpToItem && !boHeroToItem)
                {
                    n_UsesItemTick = 0;//主人英雄都没有珠子,则初始
                }
                if (this.m_nWinExp < 0)
                {
                    this.m_nWinExp = 0;
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TPlayObject.GetExpToItem Code:" + nCode);
            }
        }

        // nCode 1-内功怪
        public void GainExp(int dwExp, byte nCode)
        {
            int n;
            int sumlv;
            int nExp;
            int nHighLevel;
            int nLowLevel;
            TPlayObject PlayObject;
            int nCheckCode = 0;
            const string sExceptionMsg = "[异常] TPlayObject::GainExp Code:";
            double[] bonus = { 1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.2 };
            if (this == null)
            {
                return;
            }
            try
            {
                nHighLevel = 0;
                nLowLevel = ushort.MaxValue;
                if (dwExp > 0)
                {
                    nCheckCode = 15;
                    if ((m_GroupOwner != null))
                    {
                        nCheckCode = 9;
                        if ((m_GroupOwner.m_GroupMembers != null))
                        {
                            nCheckCode = 10;
                            if ((m_GroupOwner.m_GroupMembers.Count > 0))
                            {
                                sumlv = 0;
                                n = 0;
                                nCheckCode = 1;
                                for (var i = 0; i < m_GroupOwner.m_GroupMembers.Count; i++)
                                {
                                    PlayObject = m_GroupOwner.m_GroupMembers[i];
                                    if (PlayObject != null)
                                    {
                                        if (!PlayObject.m_boDeath && (this.m_PEnvir == PlayObject.m_PEnvir) && (Math.Abs(this.m_nCurrX - PlayObject.m_nCurrX) <= 12) && (Math.Abs(this.m_nCurrX - PlayObject.m_nCurrX) <= 12))
                                        {
                                            sumlv = sumlv + PlayObject.m_Abil.Level;
                                            if (PlayObject.m_Abil.Level > nHighLevel)
                                            {
                                                nHighLevel = PlayObject.m_Abil.Level;
                                            }
                                            if (PlayObject.m_Abil.Level < nLowLevel)
                                            {
                                                nLowLevel = PlayObject.m_Abil.Level;
                                            }
                                            n++;
                                        }
                                    }
                                }
                                nCheckCode = 2;
                                if ((sumlv > 0) && (n > 1))
                                {
                                    if (n >= 0 && n <= Grobal2.GROUPMAX)
                                    {
                                        dwExp = (int)HUtil32.Round(dwExp * bonus[n]);
                                    }
                                    nCheckCode = 3;
                                    if (m_GroupOwner.m_GroupMembers.Count > 0)
                                    {
                                        for (var i = 0; i < m_GroupOwner.m_GroupMembers.Count; i++)
                                        {
                                            PlayObject = m_GroupOwner.m_GroupMembers[i];
                                            if (PlayObject != null)
                                            {
                                                if (!PlayObject.m_boDeath && (this.m_PEnvir == PlayObject.m_PEnvir) && (Math.Abs(this.m_nCurrX - PlayObject.m_nCurrX) <= 12) && (Math.Abs(this.m_nCurrX - PlayObject.m_nCurrX) <= 12))
                                                {
                                                    if (M2Share.g_Config.boHighLevelKillMonFixExp && M2Share.g_Config.boHighLevelGroupFixExp)//在高等级经验不变时，把组队的经验平均分配
                                                    {
                                                        nCheckCode = 4;
                                                        PlayObject.WinExp((int)HUtil32.Round(Convert.ToDouble(dwExp / n)));
                                                        nCheckCode = 5;
                                                        if (nCode == 1)
                                                        {
                                                            PlayObject.GetNGExp((int)HUtil32.Round(Convert.ToDouble(dwExp / n)), 0); // 取得内力经验
                                                        }
                                                    }
                                                    else if (M2Share.g_Config.boHighLevelGroupFixExp)
                                                    {
                                                        nCheckCode = 6;
                                                        PlayObject.WinExp((int)HUtil32.Round((double)dwExp / sumlv * PlayObject.m_Abil.Level));
                                                        nCheckCode = 7;
                                                        if (nCode == 1)
                                                        {
                                                            PlayObject.GetNGExp((int)HUtil32.Round((double)dwExp / sumlv * PlayObject.m_Abil.Level), 0);  // 取得内力经验
                                                        }
                                                    }
                                                    else if (this.m_Abil.Level > (nLowLevel + 10))  // 大号杀怪组里人经验不变
                                                    {
                                                        PlayObject.WinExp((int)HUtil32.Round((double)dwExp / sumlv * PlayObject.m_Abil.Level));
                                                        if (nCode == 1)
                                                        {
                                                            PlayObject.GetNGExp((int)HUtil32.Round((double)dwExp / sumlv * PlayObject.m_Abil.Level), 0); // 取得内力经验
                                                        }
                                                    }
                                                    else if (this.m_Abil.Level <= (nLowLevel + 10))
                                                    {
                                                        if (PlayObject.m_Abil.Level > (nLowLevel + 10)) // 小号杀怪经验组里人大号经验改变
                                                        {
                                                            nExp = (int)HUtil32.Round(Convert.ToDouble(dwExp / PlayObject.m_Abil.Level));
                                                            if (nExp <= 0)
                                                            {
                                                                nExp = 1;
                                                            }
                                                            PlayObject.WinExp(nExp);
                                                            if (nCode == 1)
                                                            {
                                                                PlayObject.GetNGExp(nExp, 0);// 取得内力经验
                                                            }
                                                        }
                                                        else
                                                        {
                                                            PlayObject.WinExp((int)HUtil32.Round((double)dwExp / sumlv * PlayObject.m_Abil.Level));// 小号杀怪经验组里人小号经验不变
                                                            if (nCode == 1)
                                                            {
                                                                PlayObject.GetNGExp((int)HUtil32.Round((double)dwExp / sumlv * PlayObject.m_Abil.Level), 0);// 取得内力经验
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    nCheckCode = 11;
                                    WinExp(dwExp);
                                    if (nCode == 1)
                                    {
                                        GetNGExp(dwExp, 0);  // 取得内力经验 
                                    }
                                }
                            }
                            else
                            {
                                nCheckCode = 12;
                                WinExp(dwExp);
                                if (nCode == 1)
                                {
                                    GetNGExp(dwExp, 0); // 取得内力经验
                                }
                            }
                        }
                        else
                        {
                            nCheckCode = 13;
                            WinExp(dwExp);
                            if (nCode == 1)
                            {
                                GetNGExp(dwExp, 0);// 取得内力经验
                            }
                        }
                    }
                    else
                    {
                        nCheckCode = 14;
                        WinExp(dwExp);
                        if (nCode == 1)
                        {
                            GetNGExp(dwExp, 0);// 取得内力经验
                        }
                    }
                }
            }
            catch
            {
                M2Share.MainOutMessage(sExceptionMsg + nCheckCode);
            }
        }
    }
}